/**
 *
 */
package com.zhuoyue.elmt.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.eit.eitsite.modules.sys.entity.Dict;
import com.zhuoyue.elmt.Constants;
import com.zhuoyue.elmt.entity.FailureReport;
import com.zhuoyue.elmt.entity.GgSbjc;
import com.zhuoyue.elmt.entity.MaintenanceRecordItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.service.CrudService;
import com.zhuoyue.elmt.entity.MaintenanceRecordSz;
import com.zhuoyue.elmt.dao.MaintenanceRecordSzDao;

/**
 * 电梯维保记录Service
 *
 * @author chenhao
 * @version 2016-09-07
 */
@Service
@Transactional(readOnly = true)
public class MaintenanceRecordSzService extends CrudService<MaintenanceRecordSzDao, MaintenanceRecordSz> {

    @Autowired
    private FailureReportService failureReportService;

    @Autowired
    private GgSbjcService ggSbjcService;

    public MaintenanceRecordSz get(String id) {
        return super.get(id);
    }

    public List<MaintenanceRecordSz> findList(MaintenanceRecordSz maintenanceRecordSz) {
        return super.findList(maintenanceRecordSz);
    }

    public Page<MaintenanceRecordSz> findPage(Page<MaintenanceRecordSz> page, MaintenanceRecordSz maintenanceRecordSz) {
        return super.findPage(page, maintenanceRecordSz);
    }

    @Transactional(readOnly = false)
    public void save(MaintenanceRecordSz maintenanceRecordSz) {
        modifyRecordMainDate(maintenanceRecordSz);

        super.save(maintenanceRecordSz);
        // 更新设备维保日期
        updateSbMainDate(maintenanceRecordSz);
        // 写入故障报告
        maintenanceRecordSzToFailureRecord(maintenanceRecordSz);
        //
        maintenanceRecordSzToMainPlan(maintenanceRecordSz);
        // 更新设备经纬度
        updateSbLocation(maintenanceRecordSz);
    }

    @Transactional(readOnly = false)
    public void delete(MaintenanceRecordSz maintenanceRecordSz) {
        super.delete(maintenanceRecordSz);
    }

    public MaintenanceRecordSz getMaintenanceRecordByDeviceCode(HashMap params) {
        return dao.getMaintenanceRecordByDeviceCode(params);
    }

    public List<MaintenanceRecordSz> findMainContractMtNum(MaintenanceRecordSz maintenanceRecordSz) {
        return dao.findMainContractMtNum(maintenanceRecordSz);
    }

    @Transactional(readOnly = false)
    public void updateMaintenanceRecordByBaseInfo(MaintenanceRecordSz maintenanceRecordSz) {
        modifyRecordMainDate(maintenanceRecordSz);

        dao.updateMaintenanceRecordByBaseInfo(maintenanceRecordSz);
        // 更新设备维保日期
        updateSbMainDate(maintenanceRecordSz);
        // 写入故障报告
        maintenanceRecordSzToFailureRecord(maintenanceRecordSz);
        //
        maintenanceRecordSzToMainPlan(maintenanceRecordSz);
    }

    @Transactional(readOnly = false)
    public void updateMaintenanceRecordByMtInfo(MaintenanceRecordSz maintenanceRecordSz) {
        modifyRecordMainDate(maintenanceRecordSz);

        dao.updateMaintenanceRecordByMtInfo(maintenanceRecordSz);
        // 更新设备维保日期
        updateSbMainDate(maintenanceRecordSz);
        // 写入故障报告
        maintenanceRecordSzToFailureRecord(maintenanceRecordSz);
        //
        maintenanceRecordSzToMainPlan(maintenanceRecordSz);
    }

    @Transactional(readOnly = false)
    public void deleteMaintenanceRecordInfo(MaintenanceRecordSz record) {
        //删除维保项目
        dao.deleteMaintenanceRecordItemByRecordId(record.getRecordId());
        //删除维保记录
        dao.delete(record);
    }

    public List<Dict> findSysDictList(HashMap params) {
        return dao.findSysDictList(params);
    }

    public List<HashMap> findMaintenanceRecordItem(HashMap params) {
        return dao.findMaintenanceRecordItem(params);
    }

    @Transactional(readOnly = false)
    public void saveWbMaintenanceItemList(String recordId, ArrayList<MaintenanceRecordItem> array) {
        //删除现有的维保项目
        dao.deleteMaintenanceRecordItemByRecordId(recordId);
        //重新添加维保项目
        for (MaintenanceRecordItem model : array) {
            dao.insertMaintenanceRecordItem(model);
        }
    }

    public List<HashMap> getPrevMaintenanceRecordSz(MaintenanceRecordSz record) {
        return dao.getPrevMaintenanceRecordSz(record);
    }

    @Transactional(readOnly = false)
    public void updateOverdueType(MaintenanceRecordSz model) {
        dao.updateOverdueType(model);
    }


    private void updateSbMainDate(MaintenanceRecordSz maintenanceRecordSz) {
        // MaintenanceRecordSz的记录维保时间是设备传过来的时间
        // 设备记录的维保时间是签到时间
        if ("1".equals(maintenanceRecordSz.getStatus())) {
            // 同时更新设备的对应维保时间
            GgSbjc ggSbjc = new GgSbjc();
            ggSbjc.setSbzcdm(maintenanceRecordSz.getDeviceCode());
            ggSbjc.setNearMainDate(maintenanceRecordSz.getSignInTime());
            if ("1".equals(maintenanceRecordSz.getHalfmonthMainType())) {
                ggSbjc.setHalfmonthMainDate(maintenanceRecordSz.getSignInTime());
            }
            if ("1".equals(maintenanceRecordSz.getQuarterMainType())) {
                ggSbjc.setQuarterMainDate(maintenanceRecordSz.getSignInTime());
            }
            if ("1".equals(maintenanceRecordSz.getHalfyearMainType())) {
                ggSbjc.setHalfyearMainDate(maintenanceRecordSz.getSignInTime());
            }
            if ("1".equals(maintenanceRecordSz.getYearMainType())) {
                ggSbjc.setYearMainDate(maintenanceRecordSz.getSignInTime());
            }
            // 更新其他

            ggSbjcService.updateNearMainDate(ggSbjc);
        }
    }

    private void modifyRecordMainDate(MaintenanceRecordSz maintenanceRecordSz) {
        // 同时更新设备的对应维保时间
        GgSbjc qryGgSbjc = new GgSbjc();
        qryGgSbjc.setSbzcdm(maintenanceRecordSz.getDeviceCode());
        GgSbjc ggSbjc = ggSbjcService.getBySbzcdm(qryGgSbjc);
        if ("1".equals(maintenanceRecordSz.getHalfmonthMainType())) {
            maintenanceRecordSz.setHalfmonthMainDate(ggSbjc.getHalfmonthMainDate());
        }
        if ("1".equals(maintenanceRecordSz.getQuarterMainType())) {
            maintenanceRecordSz.setQuarterMainDate(ggSbjc.getQuarterMainDate());
        }
        if ("1".equals(maintenanceRecordSz.getHalfyearMainType())) {
            maintenanceRecordSz.setHalfyearMainDate(ggSbjc.getHalfyearMainDate());
        }
        if ("1".equals(maintenanceRecordSz.getYearMainType())) {
            maintenanceRecordSz.setYearMainDate(ggSbjc.getYearMainDate());
        }
    }

    private void updateSbLocation(MaintenanceRecordSz maintenanceRecordSz) {
        // 同时更新设备的对应维保时间
        GgSbjc ggSbjc = new GgSbjc();
        ggSbjc.setSbzcdm(maintenanceRecordSz.getDeviceCode());
        ggSbjc.setJd(maintenanceRecordSz.getLongitudeNew());
        ggSbjc.setWd(maintenanceRecordSz.getLatitudeNew());
        ggSbjcService.updateSbLocation(ggSbjc);
    }

    public void maintenanceRecordSzToFailureRecord(MaintenanceRecordSz maintenanceRecordSz) {
        // 已提交并且是不合格的才写故障报告
        // 已提交是不能再修改的 所以可以不用判断是否重复提交到FailureReport
        if ("1".equals(maintenanceRecordSz.getStatus()) && "0".equals(maintenanceRecordSz.getMaintenanceResult())) {
            FailureReport failureReport = new FailureReport();
            failureReport.setSourceNo(maintenanceRecordSz.getRecordNo());
            failureReport.setSourceType(Constants.FailureReport.SOURCE_TYPE_MAINTENANCE_RECORD); // 来源维保记录
            failureReport.setMaintenUnitCode(maintenanceRecordSz.getMaintenUnitCode()); // 维保单位
            failureReport.setMaintenanceStaff(maintenanceRecordSz.getMaintenanceStaff()); // 维保人员
            failureReport.setMaintenanceStaff1(maintenanceRecordSz.getMaintenanceStaff1());
            failureReport.setMaintenanceStaff2(maintenanceRecordSz.getMaintenanceStaff2());
            failureReport.setUseUnitCode(maintenanceRecordSz.getUseUnitCode()); // 使用单位
            failureReport.setInternalCode(maintenanceRecordSz.getInternalCode()); // 单位内部编号
            failureReport.setSafetyOfficer(maintenanceRecordSz.getSafetyOfficer()); // 安全管理人员
            failureReport.setDeviceCode(maintenanceRecordSz.getDeviceCode()); // 设备注册代码
            failureReport.setDeviceAddr(maintenanceRecordSz.getDeviceAddr()); // 设备所在地点
            failureReport.setFailureStatus(maintenanceRecordSz.getFailurePhenomenon());
            failureReportService.save(failureReport);
        }
    }

    // 写维保计划 在提交的时候 展示不用
    public void maintenanceRecordSzToMainPlan(MaintenanceRecordSz maintenanceRecordSz) {
        if ("1".equals(maintenanceRecordSz.getStatus())) {

        }
    }
}